/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file  Lin_TYpes_E3.h
 * @brief Semidrive. AUTOSAR 4.3.1 MCAL Lin
 */

#ifndef LIN_TYPES_H
#define LIN_TYPES_H

#ifdef __cplusplus
extern "C" {
#endif

#include "Std_Types.h"

#define LIN_CACHE_LINE CACHE_SIZE

#define LIN_DUMMY_STATE(x)   ((x) = (x))

#define LIN_UART_TXBREAK_BIT(bit)          ((uint32)bit - 8UL) + 0x1UL
#define LIN_UART_RXBREAK_BIT(bit)          ((uint32)bit - 9UL) + 0x1UL

#define LIN_CH_OPERATIONAL               (0xFeU)
#define LIN_CH_SLEEP_PENDING             (0xFfU)
/*      LIN_CH_SLEEP   (from Lin_StatusType)*/

typedef uint8 Lin_FramePidType;
typedef uint8 Lin_FrameDlType;

typedef enum {
    LIN_ENHANCED_CS = 0U,
    LIN_CLASSIC_CS  = 1U
}Lin_FrameCsModelType;

typedef enum {
    LIN_MASTER_RESPONSE  = 0U,    /*master to slave*/
    LIN_SLAVE_RESPONSE   = 1U,     /*slave to master*/
    LIN_SLAVE_TO_SLAVE   = 2U,     /*slave to slave*/
}Lin_FrameResponseType;

typedef struct {
    uint8                           *SduPtr;  /* Current status of channel*/
    Lin_FrameDlType                 Dl;       /* Length of current frame*/
    Lin_FrameCsModelType            Cs;       /* Checksum Model*/
    Lin_FrameResponseType           Drc;      /* Frame type*/
    Lin_FramePidType                Pid;
}Lin_PduType;

typedef enum {
    LIN_NOT_OK           = 0U,
    LIN_TX_OK            = 1U,
    LIN_TX_BUSY          = 2U,
    LIN_TX_HEADER_ERROR  = 3U,
    LIN_TX_ERROR         = 4U,
    LIN_RX_OK            = 5U,
    LIN_RX_BUSY          = 6U,
    LIN_RX_ERROR         = 7U,
    LIN_RX_NO_RESPONSE   = 8U,
    LIN_OPERATIONAL      = 9U,
    LIN_CH_SLEEP         = 10U,
} Lin_StatusType;

typedef enum {
    /* Error in header */
    LIN_ERR_HEADER           = 0U,

    /* Framing error in response */
    LIN_ERR_RESP_STOPBIT,

    /* Checksum error */
    LIN_ERR_RESP_CHKSUM,

    /* Monitoring error of transmitted data bit in response */
    LIN_ERR_RESP_DATABIT,

    /* No response */
    LIN_ERR_NO_RESP,

    /* Incomplete response */
    LIN_ERR_INC_RESP,
} Lin_SlaveErrorType;

typedef enum {
    LIN_UART_PARITY_NONE,
    LIN_UART_PARITY_EVEN,
    LIN_UART_PARITY_ODD
} lin_uart_parity_e;

typedef enum {
    LIN_UART_MASTER,
    LIN_UART_SLAVE
} lin_uart_mode_e;

typedef enum {
    LIN_UART_CLASSIC_CS,
    LIN_UART_ENHANCED_CS
} lin_uart_cs_mode_e;

typedef enum {
    LIN_UART_STOP_1BIT,
    LIN_UART_STOP_0P5BIT,
    LIN_UART_STOP_2BIT,
    LIN_UART_STOP_1P5BIT
} lin_uart_stop_e;

typedef enum {
    LIN_INITIAL_STATE,
    LIN_WAIT_FOR_WAKEUP,
    LIN_ACTIVE,
} Lin_State;

typedef enum {
    LIN_OK,
    LIN_FRAMIN_ERROR,
    LIN_PARITY_ERROR,
    LIN_OVERRUN_ERROR,
    LIN_CHKSUM_ERROR,
    LIN_LINE_ERROR
} Lin_ErrorType;

typedef enum {
    LIN_FRAMEIDLE,
    LIN_FRAMETRANSMIT,
    LIN_FRAMETRANSMITERR,
    LIN_FRAMERECEIVE,
    LIN_FRAMERECEIVEERR,
    LIN_FRAMEPENDING,
    LIN_FRAMECHECKSUMERR
} Lin_FrameState;


typedef void (*Lin_CallBackFn)(uint8 numId, uint8 *dataBuffer);
typedef void (*Lin_wakeupCallBackFn)(uint8 nodeId);

typedef struct {
    uint8            pid;
    uint8            cs;
    uint8            drc;
    uint8            dl;
    uint8            txframe[8];
    uint8            rxframe[10];
    Lin_State        currentState;
    Lin_FrameState   framestate;
    Lin_ErrorType    error;
} Lin_NodeDataType;

typedef struct {
    Lin_NodeDataType         nodeData;
    const Lin_PduType    *pduInfo;
    Lin_StatusType       ChannelState;
} Lin_ChannelInfoType;

typedef struct {
    const lin_uart_parity_e parity;
    const lin_uart_stop_e stop;
    const lin_uart_mode_e mode;
    const lin_uart_cs_mode_e cs_mode;
    const uint32 frequency;
    const uint32 baud_rate;
    const uint8 txBreakLen;
    const boolean auto_br;
    const uint8 cmd_timeout;
} lin_uart_cfg_t;

typedef struct {
    lin_uart_cfg_t dataPtr;
    const uint32 linModuleBase;
    uint8 coreId;
} Lin_Uart;

typedef enum {
    LIN_UNINIT = 0,
    LIN_INIT
} Lin_DrvStatusType;

typedef struct {
    uint8          numberOfNodes;
    const Lin_Uart      *channels;
} Lin_ConfigType;



#ifdef __cplusplus
}
#endif

#endif /* LIN_TYPES_H */
